package cn.com.git.example.common.util.security;


import java.security.MessageDigest;
import java.util.UUID;

/**
 * 编解码工具类
 */
public final class CodecUtils {

    /**
     * 获取32位唯一主键
     * UUID  可用于产生唯一的主键ID,且总是字符串
     * 随机,字符串长度固定,以便于存入数据库
     * @return
     */
    public static String get32UUID(){
        return UUID.randomUUID().toString().trim().replaceAll("-", "");
    }

    /**
     * MD5 加密
     * @param str
     * @return
     */
    public final static String MD5(String str) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F' };
        try {
            byte[] btInput = str.getBytes();
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            int j = md.length;
            char strs[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                strs[k++] = hexDigits[byte0 >>> 4 & 0xf];
                strs[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(strs);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
